package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")StrategyQuery qo) {
        model.addAttribute("page",strategyService.query(qo));
        return "/strategy/list";
    }

    @RequestMapping("/input")
    public String input(Model model, String id) {
        if (StringUtils.hasLength(id)) {
            //提供回显数据
            model.addAttribute("strategy",strategyService.findByOne(id));
        }
        //提供攻略
        model.addAttribute("catalogs",strategyCatalogService.queryCatalogVOById());
        model.addAttribute("themes",strategyThemeService.findAll());
        return "/strategy/input";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Strategy strategy) {
        strategyService.saveOrUpdate(strategy);
        return JsonResult.success();
    }

    /**
     * 删除功能
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        strategyService.delete(id);
        return JsonResult.success();
    }

}
